package com.alipay.controller;

import com.alipay.exception.BusinessException;
import com.alipay.model.Response;
import com.alipay.model.enums.ResponseCodeEnum;
import com.alipay.model.vo.DisburseReqVo;
import com.alipay.model.vo.QueryReqVo;
import com.alipay.model.vo.SendRedBagReqVo;
import com.alipay.service.RedBagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 *
 * @author Jun
 * @date 2019/3/22
 * 红包交易
 */
@Slf4j
@RestController
@RequestMapping(value = "/redbag")
public class RedBagController {

    @Autowired
    private RedBagService redBagService;

    /**
     * 申请发红包
     * @param sendRedBagReqVo
     * @return
     */
    @PostMapping("/apply")
    public Response applyRedBag(@RequestBody @Valid SendRedBagReqVo sendRedBagReqVo, HttpServletRequest request){
        String userId = getUserIdByRequest(request);
        try {
            redBagService.applyRedBag(sendRedBagReqVo, userId);
            return Response.newSuccess(sendRedBagReqVo.getRequestNo(), null);
        }catch (BusinessException e){
            // 业务报错
            log.warn("pay BusinessException, requestNo={}, e={}", sendRedBagReqVo.getRequestNo(), ExceptionUtils.getStackTrace(e));
            return Response.newFailure(sendRedBagReqVo.getRequestNo(), e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error("pay Exception, requestNo={}, e={}", sendRedBagReqVo.getRequestNo(), ExceptionUtils.getStackTrace(e));
        }
        return Response.newFailure(sendRedBagReqVo.getRequestNo(), ResponseCodeEnum.UNKNOWN_ERROR);
    }

    /**
     * 用户抢红包
     * @param disburseReqVo
     * @return
     */
    @PostMapping("/disburse")
    public Response disburse(@RequestBody @Valid DisburseReqVo disburseReqVo, HttpServletRequest request){
        String userId = getUserIdByRequest(request);
        try {
            return Response.newSuccess(disburseReqVo.getRequestNo(), redBagService.disburse(disburseReqVo, userId));
        }catch (BusinessException e){
            // 业务报错
            log.warn("disburse BusinessException, requestNo={}, e={}", disburseReqVo.getRequestNo(), ExceptionUtils.getStackTrace(e));
            return Response.newFailure(disburseReqVo.getRequestNo(), e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error("disburse Exception, requestNo={}, e={}", disburseReqVo.getRequestNo(), ExceptionUtils.getStackTrace(e));
        }
        return Response.newFailure(disburseReqVo.getRequestNo(), ResponseCodeEnum.UNKNOWN_ERROR);
    }

    /**
     * 查询红包明细
     * @param queryReqVo
     * @return
     */
    @GetMapping("/query")
    public Response query(@RequestBody @Valid QueryReqVo queryReqVo, HttpServletRequest request){
        String userId = getUserIdByRequest(request);
        try {
            return Response.newSuccess(queryReqVo.getRequestNo(), redBagService.query(queryReqVo, userId));
        }catch (BusinessException e){
            // 业务报错
            log.warn("query BusinessException, requestNo={}, e={}", queryReqVo.getRequestNo(), ExceptionUtils.getStackTrace(e));
            return Response.newFailure(queryReqVo.getRequestNo(), e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error("query Exception, requestNo={}, e={}", queryReqVo.getRequestNo(), ExceptionUtils.getStackTrace(e));
        }
        return Response.newFailure(queryReqVo.getRequestNo(), ResponseCodeEnum.UNKNOWN_ERROR);
    }

    /**
     * 根据request中的token信息解析userId
     * @param request
     * @return
     */
    private String getUserIdByRequest(HttpServletRequest request){
        // TODO 根据token解析userId
        return "";
    }
}
